// ActionScript file



import flash.events.Event;
import flash.events.SQLEvent;

import forms.Utils;
import forms.addGibForm;

import mx.collections.ArrayCollection;
import mx.collections.ArrayList;
import mx.events.CloseEvent;
import mx.events.CollectionEvent;
import mx.managers.PopUpManager;

import spark.components.DataGroup;

[Bindable]
private var popUpForm:addGibForm;

public function addGib()
{
	popUpForm=PopUpManager.createPopUp(this, addGibForm, true) as addGibForm;
	PopUpManager.centerPopUp(popUpForm);
	popUpForm.addEventListener(CloseEvent.CLOSE,addInfo);
	
}

public function initDBLink()
{
	//Utils.GibInfo.addEventListener(CollectionEvent.COLLECTION_CHANGE,infoChange_Handler,false,0,false);
	//super.addEventListener(SQLEvent.RESULT,infoChange_Handler);
}

public function addInfo(evt:CloseEvent)
{
	var arr = Utils.GibInfo.source[0];
	addGibInfo(arr.name,arr.phoneNum,arr.mphoneNum,arr.email,arr.favStatus,arr.tweetName);
	//masterList.dataProvider = arr as ArrayList;
	//masterList.validateNow();
}

public function deleteGib(evt:Event,d)
{
	trace(d.name, d.uid,masterList.selectedIndex);
	//var index = getLabelFromIndex(masterList.selectedIndex);
	
	var dObj = masterList.dataProvider.getItemAt(masterList.selectedIndex);
	deleteNow(dObj.uid);
}



public function setFav(evt:Event,data)
{
	//no filters=star, set it to false
	if(evt.currentTarget.filters.length==0)
	{
		evt.currentTarget.filters = Utils.toGray();
		changeFavStatus(data.uid,false,data);
	}
	else
	{
		evt.currentTarget.filters = [];
		changeFavStatus(data.uid,true,data);
	}
	
}

public function convertToCollection(d)
{
	var arr:ArrayCollection = new ArrayCollection;
	if(d!=null)
	{
		for each(var o in d)
		{
			arr.addItem(o);
		}
		return arr;
	}
}

public function launchOptions(evt:Event,d)
{
	trace("CLICK CAPTURED!");
	navigator.pushView(ActionsView,masterList.dataProvider.getItemAt(masterList.selectedIndex));
}


public function removeAllItems()
{
	var arr = new ArrayList();
	masterList.dataProvider = arr;
	masterList.dataGroup.dataProvider = arr;
	var dg = new DataGroup();
	//masterList.dataGroup = dg;
	Utils.GibInfo.removeAll();
	Utils.GibInfo = arr;
	Utils.GibInfo.source = [];
	masterList.validateNow();
	masterList.invalidateDisplayList();
}

//EOF....